package com.saasitems.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.saasitems.admin.dao.MediaResMapper;
import com.saasitems.admin.dao.OriginalMapper;
import com.saasitems.admin.model.Matts;
import com.saasitems.admin.model.MediaRes;
import com.saasitems.admin.model.Original;
import com.saasitems.admin.model.OriginalExample;
import com.saasitems.admin.web.common.UtilFromDb;
import com.saasitems.admin.web.vo.matts.MattsVO;
import com.saasitems.admin.web.vo.original.OriginalQueryVO;
import com.saasitems.admin.web.vo.original.OriginalVO;
import com.saasitems.admin.web.vo.rest.original.RestOriginalInfoVO;
import com.saasitems.common.model.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/11.
 */
@Service
public class OriginalService {
    @Autowired
    private OriginalMapper originalMapper;

    @Autowired
    private MediaResMapper mediaResMapper;

    public List<Original> query(OriginalQueryVO queryVO, PageInfo page) {
        OriginalExample ex = new OriginalExample();
        OriginalExample.Criteria c = ex.createCriteria();
        if (queryVO != null) {
            if (StringUtils.isNotBlank(queryVO.getTitle())) {
                c.andTitleLike("%" + queryVO.getTitle() + "%");
            }
        }

        c.andDelFlgEqualTo(0);
        ex.setOrderByClause("dsp_sort asc, create_time asc");
        // 必须存在
        c.andClassIdEqualTo(queryVO.getClassId());
        if (page != null) {
            PageHelper.startPage(page.getPageNo(), page.getPageSize());
        }
        List<Original> list = this.originalMapper.selectByExample(ex);
        if (page != null) {
            // 取count的值
            page.setTotalCount((long)((Page) list).getTotal());
        }
        return list;
    }

    @Transactional(readOnly = false)
    public void save(OriginalVO vo) {
        Original o = new Original();
        o.setClassId(vo.getClassId());
        o.setTitle(vo.getTitle());
        o.setContent(vo.getContent());
        o.setRecType(vo.getRecType());
        o.setRecId(vo.getRecId());
        o.setDspSort(vo.getDspSort());
        o.setDelFlg(0);
        o.setCreateTime(new Date());

        originalMapper.insert(o);
    }

    /**
     * 通过主键获得产品信息
     * @param id
     * @return
     */
    public OriginalVO getInfoById(Integer id) {
        Original p = originalMapper.selectByPrimaryKey(id);
        OriginalVO vo = new OriginalVO();
        BeanUtils.copyProperties(p, vo);

        if (StringUtils.isNotBlank(vo.getRecId())) {
            MediaRes ms = mediaResMapper.selectByPrimaryKey(vo.getRecId());
            vo.setMp3Name(ms.getFileName());
        }
        return vo;
    }

    public void update(OriginalVO vo) {
        Original p = originalMapper.selectByPrimaryKey(vo.getId());

        p.setId(vo.getId());
        p.setTitle(vo.getTitle());
        p.setContent(vo.getContent());
        p.setRecType(vo.getRecType());
        p.setRecId(vo.getRecId());
        p.setDspSort(vo.getDspSort());
        p.setCreateTime(new Date());

        originalMapper.updateByPrimaryKey(p);
    }

    public void delete(OriginalVO vo) {
        Original p = originalMapper.selectByPrimaryKey(vo.getId());
        p.setDelFlg(1);
        originalMapper.updateByPrimaryKey(p);
    }

    public RestOriginalInfoVO getInfoByIdAndClassId(Integer id, String classId) {

        Original p = originalMapper.selectByPrimaryKey(id);
        if (p == null || !classId.equals(p.getClassId())) {
            return null;
        }

        RestOriginalInfoVO vo = new RestOriginalInfoVO();
        vo.setKey(String.valueOf(p.getId()));
        vo.setMenu(p.getTitle());
        vo.setTitle(p.getTitle());
        vo.setContent(p.getContent());
        vo.setSou(p.getRecId());
        if (StringUtils.isNotBlank(p.getRecId())) {
            MediaRes ms = mediaResMapper.selectByPrimaryKey(p.getRecId());
            String path = UtilFromDb.getPathFromMS(ms);
            vo.setSrc(path);
        }
        return vo;
    }

    public List<RestOriginalInfoVO> getInfoByClassId(String classId) {
        OriginalExample ex = new OriginalExample();
        OriginalExample.Criteria c = ex.createCriteria();
        c.andClassIdEqualTo(classId);
        c.andDelFlgEqualTo(0);

        ex.setOrderByClause("dsp_sort asc, create_time asc");
        List<RestOriginalInfoVO> rets = new ArrayList<RestOriginalInfoVO>();
        List<Original> ogs = originalMapper.selectByExample(ex);
        for (Original p : ogs) {
            RestOriginalInfoVO vo = new RestOriginalInfoVO();

            vo.setKey(String.valueOf(p.getId()));
            vo.setMenu(p.getTitle());
            vo.setTitle(p.getTitle());
            vo.setContent(p.getContent());
            vo.setSou(p.getRecId());
            if (StringUtils.isNotBlank(p.getRecId())) {
                MediaRes ms = mediaResMapper.selectByPrimaryKey(p.getRecId());
                String path = UtilFromDb.getPathFromMS(ms);
                vo.setSrc(path);
            }
            rets.add(vo);
        }
        return rets;
    }
}
